﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;


public partial class Manage_AccountAccredit : System.Web.UI.Page, HZ.MBSM.BLL.IPageDescribe
{

    public string TableName;
    protected void Page_Load(object sender, EventArgs e)
    {
        HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
        string UserType;
        Table1.Visible = false;
        UserType = lstType.SelectedValue;
        //strBidY投标授权 是,strBidN 投标授权否 ,采购授权 是 strTradeY,采购授权 否 strTradeN
        string strBidY = "", strBidN = "", strTradeY = "", strTradeN = "", strSql;
        if (HZ.MBSM.BLL.Login.CheckAssert(this.UseAccountType))
        {
            if (!IsPostBack)
            {
                lstType.Items.Add("生产企业");
                lstType.Items.Add("经营企业");
                if (_Account.Type != "6")
                {
                    lstType.Items.Add("监督机构");
                    lstType.Items.Add("医院");
                    lstType.Items.Add("招标中介");
                }
                lstBidType.Items.Add("全部");
                lstBidType.Items.Add("已授权");
                lstBidType.Items.Add("未受权");

            }
            if (Request.Form["btnPrint"] == "打  印")
            {
                if (Request.Form["Checkbox1"] == null)
                {
                    Response.Write("<script>alert('请选择！');</script>");
                }
                else
                {
                    PrintUser();
                   
                }
            }

            if (Request.Form["btnSubmit1"] == "全部授权")
            {
                foreach (string strId in SysFun.ToTrim(Request.Form["AccountId"]).Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries))
                {
                    strBidY = strBidY + strId + ",";
                }
                if (strBidY != "")
                {
                    Db db1 = new Db();
                    strBidY = strBidY.Substring(0, strBidY.Length - 1);
                    strSql = "update account set IsBid_Landing=1 WHERE AccountId in (" + strBidY + ") ";
                    db1.Command(strSql);
                    db1.Close();
                    HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "修改", "Account", strSql, "../Magage/AccountAccredit.aspx");
                }
            }
            strBidY="";
            if (Request.Form["susSubmit"] == "初始化密码")
            {
                if (Request.Form["Checkbox1"] == null)
                {
                    Response.Write("<script>alert('请选择！');</script>");
                }
                else
                {
                    Db db1 = new Db();
                    strBidY = Request.Form["Checkbox1"].ToString();
                    strSql = "update account set Pwd_Bid='" + SysFun.PasswordEncrypt("123456") + "',Pwd_Trade='" + SysFun.PasswordEncrypt("123456") + "' WHERE AccountId in (" + strBidY + ") ";
                    db1.Command(strSql);
                    db1.Close();
                    HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "修改", "Account", strSql, "../Magage/AccountAccredit.aspx");

                    Response.Write("<script>alert('密码初始化为１２３４５６！');</script>");
                }
            }
            if (Request.Form["btnSubmit"] == "授  权")
            {
                foreach (string strId in SysFun.ToTrim(Request.Form["AccountId"]).Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries))
                {
                    if (SysFun.ToInt(Request.Form["radBid" + strId]) == 1)
                        strBidY = strBidY + strId + ",";
                    else
                        strBidN = strBidN + strId + ",";
                    if (UserType == "生产企业" || UserType == "经营企业")
                    {
                        if (SysFun.ToInt(Request.Form["radTrade" + strId]) == 1)
                            strTradeY = strTradeY + strId + ",";
                        else
                            strTradeN = strTradeN + strId + ",";
                    }
                    //Response.Write(strId + " " + Request.Form["radBid" + strId] + " " + SysFun.ToInt(Request.Form["radTrade" + strId]) + "<br>");
                }
                //Response.Write(strBidN + "<br>");
                //Response.Write(strBidY + "<br>");
                //Response.Write(strTradeY + "<br>");
                //Response.Write(strTradeN + "<br>");
                Db db = new Db();
                //分别授权
                if (strBidN != "")
                {
                    strBidN = strBidN.Substring(0, strBidN.Length - 1);
                    strSql = "update account set IsBid_Landing=0 WHERE AccountId in (" + strBidN + ") ";
                    db.Command(strSql);
                    db.Close();
                    HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "修改", "Account", strSql, "../Magage/AccountAccredit.aspx");

                }
                if (strBidY != "")
                {
                    strBidY = strBidY.Substring(0, strBidY.Length - 1);
                    strSql = "update account set IsBid_Landing=1 WHERE AccountId in (" + strBidY + ") ";
                    db.Command(strSql);
                    db.Close();
                    HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "修改", "Account", strSql, "../Magage/AccountAccredit.aspx");

                }

                if (strTradeN != "")
                {
                    strTradeN = strTradeN.Substring(0, strTradeN.Length - 1);
                    strSql = "update account set IsTrade_Landing=0 WHERE AccountId in (" + strTradeN + ") ";
                    db.Command(strSql);
                    db.Close();
                    HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "修改", "Account", strSql, "../Magage/AccountAccredit.aspx");

                }
                if (strTradeY != "")
                {
                    strTradeY = strTradeY.Substring(0, strTradeY.Length - 1);
                    strSql = "update account set IsTrade_Landing=1 WHERE AccountId in (" + strTradeY + ") ";
                    db.Command(strSql);
                    db.Close();
                    HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "修改", "Account", strSql, "../Magage/AccountAccredit.aspx");

                }
               
                showGridView();
            }
            else
            {
                showGridView();
            }
        }
    }

/// <summary>
    ///  将数据绑定到datalist
/// </summary>
    public void showGridView()
    {
        int i = 0;
        string UserType, strConditions;
        UserType = lstType.SelectedValue;
        strConditions = fctConditions();
        Db db = new Db();
        DbPage dbPage = new DbPage(db);
        dbPage.Table = "v_account";
        dbPage.Key = "accountid";
        dbPage.Where = strConditions;
        dbPage.SelectField = "accountid,accountname,DWName,pwd_bid,Pwd_trade,type, isbid_landing,istrade_landing, sort,UserCode";
        dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);
        dbPage.PageSize = 10;//Request.QueryString["PS"]);
        dbPage.OrderBy = "DWName";
        System.Text.StringBuilder strHtml = new System.Text.StringBuilder();
        strHtml.Append("<table width='98%' border='0' align='center' cellpadding='2' cellspacing='1' class='table_table'>");
        strHtml.Append("<tr>");
        strHtml.Append("<td class='table_title' nowrap>用户名称</td>");
        strHtml.Append("<td class='table_title' nowrap>用户编码</td>");
        strHtml.Append("<td class='table_title' nowrap>单位名称</td>");
        //strHtml.Append("<td class='table_title' nowrap>密  码</td>");
        strHtml.Append("<td class='table_title' nowrap>授  权</td>");
        strHtml.Append("<td class='table_title' nowrap>选择</td>");
        strHtml.Append("</tr>");
        System.Data.IDataReader dr = dbPage.ExecuteReader();
        while (dr.Read())
        {
            //if (SysFun.ToInt(dr["istrade_landing"].ToString()) == 1)
            //{
            //    strHtml.Append("<tr style='background-color:Yellow'>");
            //    i++;
            //}
            //else
            //{
                strHtml.Append("<tr class='table_td2'>");
                i--;
            //}

            strHtml.Append("<td><input type='hidden' name='AccountId' value='" + dr["accountid"].ToString() + "'>" + dr["accountname"].ToString() + "</td>");
            strHtml.Append("<td>" + dr["UserCode"].ToString() + "</td>");
            strHtml.Append("<td>" + dr["DWName"].ToString() + "</td>");
            //strHtml.Append("<td>" + SysFun.PasswordDecrypt(dr["pwd_bid"].ToString()) + "</td>");
            strHtml.Append("<td>");
            if (SysFun.ToTrim(dr["isbid_landing"]) == "1")
            {
                strHtml.Append("<input type=radio name ='radBid" + dr["accountid"].ToString() + "' value='1' checked>是");
                strHtml.Append("<input type=radio name ='radBid" + dr["accountid"].ToString() + "' value='0'>否");

            }
            else
            {
                strHtml.Append("<input type=radio name ='radBid" + dr["accountid"].ToString() + "' value='1'>是");
                strHtml.Append("<input type=radio name ='radBid" + dr["accountid"].ToString() + "' value='0' checked>否");

            }

            strHtml.Append("</td>");
            strHtml.Append("<td>");
            strHtml.Append(" <input name=\"Checkbox1\" value='" + dr["accountid"].ToString() + "'  type=\"radio\" /> ");
            strHtml.Append("</td>");
            strHtml.Append("</tr>");
        }
        strHtml.Append("</table>");
        Label4.Text = strHtml.ToString();
        dr.Close();
        dr.Dispose();
        dbPage.Db.Close();
        db.Close();

        this.DbPageFlip1.DbPage = dbPage;


    }
    /// <summary>
    /// 生成查询条件
    /// </summary>
    /// <returns></returns>
    public string fctConditions()
    {
        string strtemp = "", AccountName, DWName, UserCode;
        AccountName = txtAccountName.Text.ToString().Trim();
        DWName = txtDWName.Text.ToString().Trim();
        UserCode = SysFun.ToTrim(txtUserCode.Text);
        switch (lstType.SelectedValue.ToString())
        {
            case "生产企业":
                strtemp = " sort=0 and type=2 and";
                TableName = "v_account_Company";
                break;
            case "经营企业":
                strtemp = " sort=1 and type=2 and";
                TableName = "v_account_Company";
                break;
            case "监督机构":
                strtemp = " type=4 and";
                TableName = "v_account_Supervise";
                break;
            case "医院":
                strtemp = " type=3 and";
                TableName = "v_account_Hospital";
                break;
            case "招标中介":
                strtemp = " type=1 and";
                TableName = "v_account_Attorney";
                break;
        }
        switch (lstBidType.SelectedValue.ToString())
        {
            case "全部":
                break;
            case "已授权":
                strtemp += " isbid_landing=1 and";
                break;
            case "未受权":
                strtemp += " (isbid_landing=0 or isbid_landing is null) and";
                break;
        }
        string _strPrintStatus = lstPrint.SelectedValue;
        switch (_strPrintStatus)
        {
            case "0":
                strtemp += " (istrade_landing=0 or istrade_landing is null) and";
                break;
            case "1":
                strtemp += " istrade_landing=1 and";
                break;
            case "2":

                break;
        }

        if (UserCode != "")
        {
            strtemp = strtemp + " UserCode like '%" + UserCode + "%' and";
        }
        if (AccountName != "")
        {
            strtemp = strtemp + " AccountName like '%" + AccountName + "%' and";
        }
        if (DWName != "")
        {
            strtemp = strtemp + " DWName like '%" + DWName + "%' and";
        }
        strtemp = "  " + strtemp.Substring(0, strtemp.Length - 3);
        return strtemp;

    }

    protected void PrintUser()
    {
        Table1.Visible = true;
        string _strAccountId = Request.Form["Checkbox1"].ToString();
        Db db1 = new Db();
        db1.Command("update account set istrade_landing=1 where accountid=" + _strAccountId);
       
        string _strSql = "select * from v_account where accountid=" + _strAccountId;
        System.Data.IDataReader dr = db1.DataReader(_strSql);
        if (dr.Read())
        {
            TableCell22.Text = dr["DWName"].ToString();
            TableCell8.Text = dr["AccountName"].ToString();
            TableCell9.Text = SysFun.PasswordDecrypt(dr["Pwd_bid"].ToString());
            TableCell10.Text = "企业登录系统之后，可自己修改密码。";
            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
            TableCell11.Text = _Account.AccountName;
        }

        dr.Close();        
        db1.Close();              
        //将合同导入到word中
        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "GB2312";
       
        // 添加头信息，为"文件下载/另存为"对话框指定默认文件名 
        Response.AddHeader("Content-Disposition", "attachment; filename=UserInfo.doc");
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
        Response.ContentType = "application/ms-word";
        this.EnableViewState = false;
        System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
        this.Table1.RenderControl(oHtmlTextWriter);

        // 把文件流发送到客户端 
        Response.Write(oStringWriter.ToString());
        // 停止页面的执行

        Response.End();

    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        //OverRide　为了使导出成Excel可行！
    }
    #region IPageDescribe 成员

    public string Name
    {
        get { return "权限分配"; }
    }

    public string Describe
    {
        get { return ""; }
    }

    public string ParentName
    {
        get { return "用户管理"; }
    }

    public System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> UseAccountType
    {
        get
        {
            System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
            _AccountType.Add(HZ.MBSM.DAL.AccountType.enumAdmin);
            _AccountType.Add(HZ.MBSM.DAL.AccountType.enumCheckCompany);
            return _AccountType;
        }
    }

    #endregion



}
